package com.yulp.springboot2task.config;

import org.springframework.context.annotation.Bean;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;

import java.util.concurrent.ThreadPoolExecutor;

/**
 * @description:
 * @date: 2019/11/4 12:22.
 * @author: yulipeng.
 */
@Component
public class BeanInit {

    /**
     * spring 线程池
     * @return
     */
    @Bean("threadPool")
    //@Qualifier("threadPool") 使用方式
    public ThreadPoolTaskExecutor executor() {
        System.out.println("==========创建spring线程池=======");
        ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
        executor.setCorePoolSize(3);
        executor.setMaxPoolSize(15);
        executor.setKeepAliveSeconds(1);
        executor.setQueueCapacity(50);
        executor.setRejectedExecutionHandler(new ThreadPoolExecutor.CallerRunsPolicy());
        executor.initialize();
        /*executor.execute(new Runnable() {
            @Override
            public void run() {
                //执行的代码
            }
        });*/
        return executor;
    }
}
